//
//  SetViewController.swift
//  YunCang
//
//  Created by wangshuning on 16/7/19.
//  Copyright © 2016年 wangshuning. All rights reserved.
//

import UIKit

import MMDrawerController

class SetViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
    
    var tableView: UITableView?
    var drawerController: MMDrawerController?
    var array = [[String:String]]()
    var allInfo: [String : String] = ["大":"18","中":"15","小":"12"]
    var dataArray: [[String : String]] = [["name" : "大","font":"18"],["name":"中","font":"15"],["name":"小","font":"12"]]
    var dataArr = [[String]]()
    //#MARK: – Lifecycle
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        let nav = UINavigationBar.init(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: 64))
        UINavigationBar.appearance().barTintColor = UIColor.hexStringToUIColor("#00acff")
       
        let lbutton = UIButton.init(frame: CGRect(x: 15, y: 20, width: 44, height: 44))
        lbutton.setImage(UIImage(named: "返回"), for: UIControlState())
        //        lbutton.backgroundColor = UIColor.greenColor()
        lbutton.imageView?.contentMode = UIViewContentMode.scaleAspectFill
        lbutton.addTarget(self, action: #selector(SetViewController.back), for: UIControlEvents.touchUpInside)
        
        let label = UILabel.init(frame: CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44))
        label.text = "设置"
//        label.backgroundColor = UIColor.redColor()
        label.textAlignment = NSTextAlignment.center
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 17)
    
        self.view.addSubview(nav)
        self.view.addSubview(lbutton)
        self.view.addSubview(label)

        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 64, width: KscreenWidth, height: KscreenHeight))
        tableView!.backgroundColor = UIColor.hexStringToUIColor("#eeeeee")
//        tableView!.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "set")
        tableView!.register(UINib(nibName: "SetTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "SetTableViewCell")
        tableView!.delegate = self
        tableView?.dataSource = self
        self.view.addSubview(tableView!)
        dataArr=[["服务器信息","字号设置","是否自动检测版本更新"],["关于云仓","帮助"]]
    }
    
    func back()  {
        
        //设置中间视图
        let app = UIApplication.shared
        let app2 = app.delegate;
        let  yunCVC = YunCangViewController()
        let  centerNVC = YunCenterViewController(rootViewController: yunCVC)
        //设置左边抽屉视图
        let leftVC = YunLeftTableViewController()
        //对抽屉效果进行设置
        drawerController = MMDrawerController.init(center: centerNVC, leftDrawerViewController: leftVC)
        drawerController?.showsShadow = false
        drawerController?.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.all
        drawerController?.maximumLeftDrawerWidth = 270
        app2?.window!!.rootViewController = drawerController
    }
    
//    func upDateValue() {
//        
//        print("fdfdfdg")
//    }
    //MARK: - delegate
    func clickSwitch(_ sender: UISwitch){
        userInfo?.isAutomicUpdate = sender.isOn
    }
    //#MARK: – UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr[section].count 
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataArr.count
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SetTableViewCell", for: indexPath)as!SetTableViewCell
        cell.rightLabel?.text = dataArr[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
        if  (indexPath as NSIndexPath).section == 0 {
            if (indexPath as NSIndexPath).row == 1 {
                var fontName = ""
                switch userInfo!.fontLevel {
                case .normal:
                    fontName = "默认"
                case .middle:
                    fontName = "中号"
                case .large:
                    fontName = "大号"
                }
                cell.leftLabel.text = fontName
                cell.checkSwitch.isHidden = true
                cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
            }
            if (indexPath as NSIndexPath).row == 0 {
                cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
                cell.checkSwitch.isHidden = true
            }
            if (indexPath as NSIndexPath).row == 2 {
                cell.checkSwitch.addTarget(self, action: #selector(SetViewController.clickSwitch(_:)), for: UIControlEvents.valueChanged)
                cell.checkSwitch.isOn = (userInfo?.isAutomicUpdate)!
                cell.selectionStyle = UITableViewCellSelectionStyle.none
                return cell
            }
        }
        cell.checkSwitch.isHidden = true
        cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        return cell
    }
    
    //#MARK: – UITableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (indexPath as NSIndexPath).section == 0 {
            if (indexPath as NSIndexPath).row == 0 {
                let IPVC = IPViewController()
                self.navigationController?.pushViewController(IPVC, animated: true)
                //            self.presentViewController(serverVC, animated: true, completion: nil)
            }else if (indexPath as NSIndexPath).row == 1{
                let selectorView = HDFontView(dataArray: array)
                selectorView.showSelectorHD(self.tableView!, seletedRow: userInfo!.fontLevel.rawValue , success: { (num) in
                    userInfo?.fontLevel = FontLevel(rawValue: num)!
                    self.tableView?.reloadData()
                })
            }else{
              return
            }

        }else{
            if (indexPath as NSIndexPath).row == 0{
                let aboatVC = AboatViewController()
                self.navigationController?.pushViewController(aboatVC, animated: true)
            }else{
                return
            }
        }
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 { //如果不需要显示文字，首行5点高度
            return 20
        }else{
            return 30
        }
    }

    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
        if section == 0 { //如果不需要显示文字，首行5点高度
            return 0
        }else{
            return 0.5
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
